﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using Common;
using DevExpress.Xpo;
using BusinessObjects;
using System.Linq;

namespace UI
{
    public partial class frmMain : RibbonBase
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            try
            {
                //DevExpress.Xpo.XpoDefault.DataLayer = DevExpress.Xpo.XpoDefault.GetDataLayer(System.Configuration.ConfigurationManager.ConnectionStrings["connect"].ConnectionString, DevExpress.Xpo.DB.AutoCreateOption.DatabaseAndSchema);
                //DevExpress.Xpo.Session.DefaultSession.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
                DevExpress.Xpo.Session.DefaultSession.ConnectionString = DevExpress.Xpo.DB.AccessConnectionProvider.GetConnectionString(Application.StartupPath + @"\DB\QLSVThucTap.mdb");
                DevExpress.Xpo.Session.DefaultSession.Connect();


                InitData();
                ShowLogin();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void InitData()
        {
            using (NestedUnitOfWork nuow = DevExpress.Xpo.Session.DefaultSession.BeginNestedUnitOfWork())
            {
                var obj = nuow.Query<TaiKhoan>().FirstOrDefault(c => c.TenTaiKhoan == "admin");
                if (obj == null)
                {
                    obj = new TaiKhoan(nuow);
                    obj.TenTaiKhoan = "admin";
                    obj.MatKhau = "phung";
                    obj.Ho = "admin";
                    obj.Ten = string.Empty;
                    obj.QuanTri = true;
                    nuow.CommitChanges();
                }
            }
        }

        private void ShowLogin()
        {
            this.Hide();
            GlobalValues.RefreshValues();
            frmLogin frm = new frmLogin();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                this.Show();
                this.WindowState = FormWindowState.Maximized;
            }
            else
                this.Close();
        }

        private void ShowForm(FormBase frm)
        {
            int index = -1;
            for (int i = 0; i < this.MdiChildren.Length; i++)
            {
                if (this.MdiChildren[i].Name == frm.Name)
                {
                    index = i;
                    break;
                }
            }
            if (index >= 0)
            {
                this.MdiChildren[index].Activate();
            }
            else
            {
                frm.MdiParent = this;
                frm.Show();
            }
        }

        private void mnuLinhVuc_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                ShowForm(new frmLinhVuc());             
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void mnuCongTy_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                ShowForm(new frmCongTy());
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void mnuBoPhan_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                ShowForm(new frmBoPhan());
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void mnuNamThucTap_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                ShowForm(new frmNamThucTap());
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void mnuKhoa_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                ShowForm(new frmKhoa());
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void mnuNganhHoc_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                ShowForm(new frmNganhHoc());
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void mnuTaiKhoan_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                ShowForm(new frmTaiKhoan());
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void mnuChangePass_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                frmChangePass frm = new frmChangePass();
                frm.ShowDialog();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void mnuLogout_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                ShowLogin();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void mnuThietLapChoNam_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                ShowForm(new frmThietLapChoNam());
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void mnuKetQuaThucTap_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                ShowForm(new frmKetQuaHocTap());
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void mnuGiangVien_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                ShowForm(new frmGiangVien());
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void mnuSinhVien_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                ShowForm(new frmSinhVien());
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void mnuTinhThanh_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                ShowForm(new frmTinhThanh());
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void mnuQuanHuyen_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                ShowForm(new frmQuanHuyen());
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void mnuReports_SLSVTrongCongTy_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                ShowForm(new frmReports_SLSVTrongCongTy());
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void mnuThoat_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.Close();
        }

        private void mnufrmReports_SVTrongNganh_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                ShowForm(new frmReports_SVTrongNganh());
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void mnufrmReports_SVTrongGV_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                ShowForm(new frmReports_SVTrongGV());
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }
    }
}